package org.scboy.dbms.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.BaseRequestValidConfigDao;
import org.scboy.dbms.dao.BaseRequestValidDao;
import org.scboy.dbms.dao.BaseRequestValidParamsDao;
import org.scboy.dbms.service.RequestValidService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;

@Service
public class RequestValidServiceImpl implements RequestValidService {

	@Autowired
	EntityFactory factory;
	
	/**
	 * 分页查询验证配置
	 * @param params
	 * @return
	 */
	public Page page(Model params){
		BaseRequestValidDao dao=factory.getDao(BaseRequestValidDao.class);
		params.orderBy("url");
		return dao.page(params);
	}
	
	/**
	 * 获取验证配置
	 * @param id
	 * @return
	 */
	public Model get(String id){
		BaseRequestValidDao dao=factory.getDao(BaseRequestValidDao.class);
		return dao.get(Model.create().add("id", id));
	}
	
	/**
	 * 新增验证配置
	 * @param model
	 * @return
	 */
	public boolean add(Model model){
		BaseRequestValidDao dao=factory.getDao(BaseRequestValidDao.class);
		if(StrUtil.isEmpty(model.getString("code"))) {
			model.add("code", "default");
		}
		Long count=dao.count(
				Model.create().add("url", model.getString("url"))
				.add("code",  model.getString("code")));
		if(count>0){
			return false;
		}
		model.add("id", UUID.randomUUID().toString());
		model.add("activity", false);
		int result=dao.insert(model);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新验证配置信息
	 * @param model
	 * @return
	 */
	public boolean update(Model model){
		BaseRequestValidDao dao=factory.getDao(BaseRequestValidDao.class);
		int result=dao.update(model,Model.create().add("id", model.getString("id")));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 删除验证配置信息
	 * @param id
	 * @return
	 */
	@Transactional
	public boolean delete(String id){
		BaseRequestValidDao dao=factory.getDao(BaseRequestValidDao.class);
		BaseRequestValidParamsDao paramsDao=factory.getDao(BaseRequestValidParamsDao.class);
		BaseRequestValidConfigDao configDao=factory.getDao(BaseRequestValidConfigDao.class);
		List<Model> validParams=paramsDao.select(Model.create().add("rvid", id));
		for (Model model : validParams) {
			configDao.delete(Model.create().add("rvpid", model.getString("id")));
			paramsDao.delete(Model.create().add("id", model.getString("id")));
		}
		dao.delete(Model.create().add("id", id));
		return true;
	}
	
	/**
	 * 启用禁用
	 * @param id ID
	 * @return
	 */
	public boolean checkState(String id) {
		Model model = get(id);
		if (model == null) {
			return false;
		}
		model.add("activity", !model.getBoolean("activity"));
		BaseRequestValidDao dao=factory.getDao(BaseRequestValidDao.class);
		int resultCount = dao.update(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}
	

	/**
	 * 获取验证列
	 * @param url
	 * @param code
	 * @return
	 */
	public List<Model> rules(String url,String code){
		BaseRequestValidDao validDao=factory.getDao(BaseRequestValidDao.class);
		BaseRequestValidParamsDao paramsDao=factory.getDao(BaseRequestValidParamsDao.class);
		BaseRequestValidConfigDao configDao=factory.getDao(BaseRequestValidConfigDao.class);
		
		//检查是否有配置
		Model valid=validDao.get(Model.create().add("url", url).add("code", code).add("activity", true));
		if(valid!=null) {
			List<Model> paramsList=paramsDao.select(Model.create().add("rvid", valid.getString("id")).add("activity", true));
			List<Model> list=new ArrayList<Model>();
			for (Model model : paramsList) {
				List<Model> configs=configDao.select(Model.create().add("rvpid", model.getString("id")));
				if(configs.size() > 0) {
					model.add("configs", configs);
					list.add(model);
				}
			}
			return list;
		}else {
			return new ArrayList<Model>();
		}
	}
	
	/**
	 * 复制验证配置信息
	 * @param id
	 * @return
	 */
	@Transactional
	public boolean copy(String id){
		BaseRequestValidDao dao=factory.getDao(BaseRequestValidDao.class);
		BaseRequestValidParamsDao paramsDao=factory.getDao(BaseRequestValidParamsDao.class);
		BaseRequestValidConfigDao configDao=factory.getDao(BaseRequestValidConfigDao.class);
		
		String newId=UUID.randomUUID().toString();
		
		//复制
		Model copy=dao.get(Model.create().add("id", id));
		copy.add("id", newId);
		copy.add("code", "default"+DateUtil.format(new Date(), "yyyyMMddHHmmss"));
		dao.insert(copy);
		//复制参数
		List<Model> validParams=paramsDao.select(Model.create().add("rvid", id));
		for (Model params : validParams) {
			
			List<Model> configs=configDao.select(Model.create().add("rvpid", params.getString("id")));

			String newParamsId=UUID.randomUUID().toString();
			
			for (Model config : configs) {
				String newConfigId=UUID.randomUUID().toString();
				config.add("id", newConfigId);
				config.add("rvpid", newParamsId);
				configDao.insert(config);
			}
			
			params.add("id", newParamsId);
			params.add("rvid", newId);
			paramsDao.insert(params);
		}
		return true;
	}
	
}
